var contextPath = "";
$(function () {
    contextPath = getContextPath();
    inittemplate();
    $(".btn").click(function () {
        var name = $(this).val();
        var url=contextPath+"/Template/ChangeTemplate.press";
        var data={};
        data.template_id=name;
        $.post(url,data,function(result){
            if(result.result){
                alert("修改成功");
                location.reload();
            }else{
                alert("修改失败");
            }
        },"json");
    });
});

function inittemplate() {
    $.ajaxSetup({
        async:false
    });
    var url=contextPath+"/Template/ShowAllTemplates.press";
    var data={};
    $.post(url,data,function (result) {
        var content="";
        $("#number").html(result.templates.length);
        for(var i=0;i<result.templates.length;i++){
            var template_id=result.templates[i].template_id;
            var template_file=result.templates[i].template_file==null?"":result.templates[i].template_file;
            content+="<div class='col-md-4 col-sm-6 col-xs-12'> " +
                "<div class='box box-solid'> <div class='template-item' > " +
                "<div class='template-item-image'><img  src='"+contextPath+template_file+"/tpl_screenshot.png'> " +
                "</div> <div class='template-item-footer cf'> <span class='template-item-name'>"
                +template_id+"</span> <button class='btn btn-default btn-sm pull-right' value='"+template_id+"'";
            if(result.isusetemplate.template_id==template_id){
                content+=" disabled='disabled'>已启用</button></div> </div> </div> </div>";
            }else{
                content+=">启用</button></div> </div> </div> </div>";
            }
            $("#content").html(content);
        }
    },"json");
}
